from sqlalchemy import exc, or_
from sqlalchemy.orm import Session

from crud import StudentAdminCrud
from model.TeachingModel import InterviewContentModel, InterviewModel
from schema.QuerySchema import QueryBase, InterviewQuery
from schema.TeachingSchema import InterviewContentInfo, InterviewInfo
from util.ErrorCode import ErrorCode
from util.LogHelper import Log


def add_interview(interviewInfo: InterviewInfo, db: Session):
    class_id = StudentAdminCrud.query_student_class_id_by_id(interviewInfo.student_id, db)
    if class_id == None:
        return {'code': ErrorCode.STUDENT_NOT_EXISTS.value, 'message': 'student not exists!'}
    else:
        interviewModel = InterviewModel(class_id=class_id, student_id=interviewInfo.student_id, company_name=interviewInfo.company_name,
                                        work_location=interviewInfo.work_location,
                                        interview_type=interviewInfo.interview_type,
                                        interview_time=interviewInfo.interview_time,
                                        interview_rounds=interviewInfo.interview_rounds,
                                        interview_result=interviewInfo.interview_result,
                                        interview_record=interviewInfo.interview_record,
                                        lecturer_id=interviewInfo.lecturer_id,
                                        lecturer_comment=interviewInfo.lecturer_comment)
        try:
            db.add(interviewModel)
            db.commit()
            return {'code': ErrorCode.SUCCESS.value, 'message': 'success'}
        except exc.SQLAlchemyError as e:
            db.rollback()
            Log.getlogger().error(f'add_interview failed:{interviewInfo} message: {e}')
            return {'code': ErrorCode.DATABASE_ERROR.value, 'message': 'database error'}


def mod_interview(interviewInfo: InterviewInfo, db: Session):
    interviewModel = db.query(InterviewModel).filter(InterviewModel.id == interviewInfo.id).one_or_none()
    if interviewModel is not None:
        if interviewInfo.student_id != 0:
            interviewModel.student_id = interviewInfo.student_id
            class_id = StudentAdminCrud.query_student_class_id_by_id(interviewInfo.student_id, db)
            if class_id != None:
                interviewModel.class_id = class_id
            else:
                return {'code': ErrorCode.STUDENT_NOT_EXISTS.value, 'message': 'student not exists!'}
        if interviewInfo.interview_type != 0:
            interviewModel.interview_type = interviewInfo.interview_type
        if interviewInfo.company_name is not None:
            interviewModel.company_name = interviewInfo.company_name if len(
                interviewInfo.company_name) != 0 else interviewModel.company_name
        if interviewInfo.work_location is not None:
            interviewModel.work_location = interviewInfo.work_location if len(
                interviewInfo.work_location) != 0 else interviewModel.work_location
        if interviewInfo.interview_time is not None:
            interviewModel.interview_time = interviewInfo.interview_time
        if interviewInfo.interview_rounds != 0:
            interviewModel.interview_rounds = interviewInfo.interview_rounds
        if interviewInfo.interview_result != 0:
            interviewModel.interview_result = interviewInfo.interview_result
        if interviewInfo.interview_record is not None:
            interviewModel.interview_record = interviewInfo.interview_record if len(
                interviewInfo.interview_record) != 0 else interviewModel.interview_record
        if interviewInfo.lecturer_id != 0:
            interviewModel.lecturer_id = interviewInfo.lecturer_id
        if interviewInfo.lecturer_comment is not None:
            interviewModel.lecturer_comment = interviewInfo.lecturer_comment if len(
                interviewInfo.lecturer_comment) != 0 else interviewModel.lecturer_comment

        try:
            db.commit()
            return {'code': ErrorCode.SUCCESS.value, 'message': 'success'}
        except exc.SQLAlchemyError as e:
            Log.getlogger().error(f'mod_interview failed:{interviewInfo} message: {e}')
            return {'code': ErrorCode.DATABASE_ERROR.value, 'message': 'database error'}
    else:
        Log.getlogger().error(f'id:{interviewInfo.id} is not exists!')
        return {'code': ErrorCode.DATABASE_RECORD_NOT_FOUND.value, 'message': 'record not exists!'}


def query_interview_by_field(interviewQuery: InterviewQuery, db: Session):
    try:
        total_count = db.query(InterviewModel).filter(InterviewModel.student_id == interviewQuery.student_id).count()
        total_pages = (total_count + interviewQuery.query_base.limit - 1) // interviewQuery.query_base.limit

        result = db.query(InterviewModel).filter(InterviewModel.student_id == interviewQuery.student_id).offset((interviewQuery.query_base.skip - 1) * interviewQuery.query_base.limit).limit(interviewQuery.query_base.limit).all()

        return {'code': ErrorCode.SUCCESS.value, 'message': 'success', 'total_count': total_count, 'total_pages': total_pages, 'data': result}
    except exc.SQLAlchemyError as e:
        Log.getlogger().error(
            f'query_interview_by_field failed! student_id: {interviewQuery.student_id} message: {e}')
        return {'code': ErrorCode.DATABASE_ERROR.value, 'message': 'database error'}


def query_interview_by_page(query_base: QueryBase, db: Session):
    try:
        total_count = db.query(InterviewModel).count()
        total_pages = (total_count + query_base.limit - 1) // query_base.limit

        result = db.query(InterviewModel).offset((query_base.skip - 1) * query_base.limit).limit(query_base.limit).all()
        return {'code': ErrorCode.SUCCESS.value, 'message': 'success', 'total_count': total_count, 'total_pages': total_pages, 'data': result}
    except exc.SQLAlchemyError as e:
        Log.getlogger().error(f'query_interview_by_page failed! message: {e}')
        return {'code': ErrorCode.DATABASE_ERROR.value, 'message': 'database error'}


def add_interview_content(interviewContentInfo: InterviewContentInfo, db: Session):
    interviewContentModel = InterviewContentModel(interview_id=interviewContentInfo.interview_id,
                                                  question=interviewContentInfo.question,
                                                  answer=interviewContentInfo.answer)
    try:
        db.add(interviewContentModel)
        db.commit()
        return {'code': ErrorCode.SUCCESS.value, 'message': 'success'}
    except exc.SQLAlchemyError as e:
        db.rollback()
        Log.getlogger().error(f'add_interview_content failed:{interviewContentInfo} message: {e}')
        return {'code': ErrorCode.DATABASE_ERROR.value, 'message': 'database error'}


def mod_interview_content(interviewContentInfo: InterviewContentInfo, db: Session):
    interviewContentModel = db.query(InterviewContentModel).filter(
        InterviewContentModel.id == interviewContentInfo.id).one_or_none()
    if interviewContentModel is not None:
        if interviewContentInfo.interview_id != 0:
            interviewContentModel.interview_id = interviewContentInfo.interview_id
        if interviewContentInfo.question is not None:
            interviewContentModel.question = interviewContentInfo.question if len(
                interviewContentInfo.question) != 0 else interviewContentModel.question
        if interviewContentInfo.answer is not None:
            interviewContentModel.answer = interviewContentInfo.answer if len(
                interviewContentInfo.answer) != 0 else interviewContentModel.answer

        try:
            db.commit()
            return {'code': ErrorCode.SUCCESS.value, 'message': 'success'}
        except exc.SQLAlchemyError as e:
            Log.getlogger().error(f'mod_interview_content failed:{interviewContentInfo} message: {e}')
            return {'code': ErrorCode.DATABASE_ERROR.value, 'message': 'database error'}
    else:
        Log.getlogger().error(f'id:{interviewContentInfo.id} is not exists!')
        return {'code': ErrorCode.DATABASE_RECORD_NOT_FOUND.value, 'message': 'record not exists!'}


def query_interview_content_by_field(interview_id: int, db: Session):
    try:
        result = db.query(InterviewContentModel).filter(
            or_(InterviewContentModel.interview_id == interview_id, interview_id == 0)).all()
        return {'code': ErrorCode.SUCCESS.value, 'message': 'success', 'data': result}
    except exc.SQLAlchemyError as e:
        Log.getlogger().error(f'query_interview_content_by_field failed! interview_id: {interview_id}  message: {e}')
        return {'code': ErrorCode.DATABASE_ERROR.value, 'message': 'database error'}
